--LLVM二进制文件生成可执行文件，包含优化。
local dependenceFileName = "Dependence.txt"
local workDir = "."
local targetTable={}
if nil ~= globalStringTargetDependenceFile then
	dependenceFileName = globalStringTargetDependenceFile
	workDir = globalStrRootDir.."/llmake-bc-target"
	targetTable = globalTableTargetDependenceTable
else
	print("read from "..dependenceFileName)
	os.exit()
end
require(globalStrModuleUtilities)

for i,v in pairs(targetTable) do
	local targetTokenTable = parseString2Token(v,";")
	local targetBCName = ""
	local targetAsmName = ""
	local targetObjName = ""
	local targetName = ""
	local linkCommandStr=""
	local execLinke =false
	if "type=EXEC" == targetTokenTable[2] then
		targetBCName = workDir.."/"..targetTokenTable[1]..".bc"
		targetAsmName = workDir.."/"..targetTokenTable[1]..".asm"
		targetObjName = workDir.."/"..targetTokenTable[1]..".obj"
		targetName = workDir.."/"..targetTokenTable[1]
		linkCommandStr = NATIVE_CXXLD.." -o "
		execLinke = true
	elseif "type=STATIC" == targetTokenTable[2] then
		targetBCName = workDir.."/lib"..targetTokenTable[1]..".bc"
		targetAsmName = workDir.."/lib"..targetTokenTable[1]..".asm"
		targetObjName = workDir.."/lib"..targetTokenTable[1]..".obj"
		targetName = workDir.."/lib"..targetTokenTable[1]..".a"
		linkCommandStr = NATIVE_AR.." r "
	elseif "type=SHARED" == targetTokenTable[2] then
		targetBCName = workDir.."/lib"..targetTokenTable[1]..".bc"
		targetAsmName = workDir.."/lib"..targetTokenTable[1]..".asm"
		targetObjName = workDir.."/lib"..targetTokenTable[1]..".obj"
		targetName = workDir.."/lib"..targetTokenTable[1]..".so"
		linkCommandStr = NATIVE_CXXLD.." -shared -o "
		execLinke = true
	end

	local optCmdStr = LLVM_OPT..targetBCName.." -o "..targetBCName
	print(optCmdStr)
	local ret = os.execute(optCmdStr)
	if true == globalBoolStopAtExecError then
		if 0 ~= ret then
			os.exit(-1)
		end
	end

	local llcCmdStr = LLVM_LLC..targetBCName.." -o "..targetAsmName
	print(llcCmdStr)
	local ret = os.execute(llcCmdStr)
	if true == globalBoolStopAtExecError then
		if 0 ~= ret then
			os.exit(-1)
		end
	end

	local asCmdStr = NATIVE_AS..targetAsmName.." -o "..targetObjName
	print(asCmdStr)
	local ret = os.execute(asCmdStr)
	if true == globalBoolStopAtExecError then
		if 0 ~= ret then
			os.exit(-1)
		end
	end


	local ldCmdStr = linkCommandStr..targetName
	if execLinke then
		ldCmdStr = ldCmdStr..NATIVE_EXEC_LINK_PREFIX
		ldCmdStr = ldCmdStr..targetObjName
		ldCmdStr = ldCmdStr..NATIVE_EXEC_LINK_SUFIX
		local defaultLDFlags = globalTableSystemCompilerOption["ldflags"]
		ldCmdStr = ldCmdStr..defaultLDFlags
		local requiredLDFlags=""
		for i,v in pairs(gTableParseTargetData[gTableRequiredPackage]) do
			local index = string.lower(v).."_ldflags"
			local tmpCflags = globalTableSystemCompilerOption[index]
			if nil ~= tmpCflags then
				requiredLDFlags = requiredLDFlags..tmpCflags
			end
		end
		ldCmdStr = ldCmdStr..requiredLDFlags
		local libsSections= targetTokenTable[4]
		if nil ~= libsSections then 
			local libsName = string.sub(libsSections,5)
			local libsOptions = " -l"
			if nil ~= libsName then 
				local libsTable = parseString2Token(libsName," ")
				if 1 <= table.maxn(libsTable) then
					libsOptions = libsOptions..table.concat(libsTable," -l")
					ldCmdStr = ldCmdStr..libsOptions
				end
			end
		end
	else
		ldCmdStr = ldCmdStr.." "..targetObjName
	end
	print(ldCmdStr)
	local ret = os.execute(ldCmdStr)
	if true == globalBoolStopAtExecError then
		if 0 ~= ret then
			os.exit(-1)
		end
	end
end
